/*
 * 类名称:ShopServiceImpl.java
 * 包名称:com.unimall.graded.app.api.shop
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-03-17 09:48:29        lin     初版做成
 *
 * Copyright (c) 2019-2019 浵浵科技
 */
package com.unimall.graded.app.api.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.unimall.graded.data.domain.ShopDO;
import com.unimall.graded.data.mapper.ShopMapper;
import com.iotechn.unimall.data.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.apache.ibatis.session.RowBounds;

import java.util.Date;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 分销用户商铺ServiceImpl实现类
 *
 * @author lin
 * @date 2020-03-17 09:48:29
 */
@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addShop(ShopDO shopDO) throws ServiceException{
        if (shopDO == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
          if( shopDO.getUserId() == null ){
            throw new AdminServiceException("unimall的用户id不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
         if( StringUtils.isEmpty(shopDO.getShopName()) ){
            throw new AdminServiceException("商铺名称不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
    if(shopMapper.selectCount(new EntityWrapper<ShopDO>().eq("shop_name",shopDO.getShopName())) > 0){
        throw new AdminServiceException("商铺名称数据库中已存在",ExceptionDefinition.PARAM_IS_ALREADY_EXIST_CODE);
    }
               
        Date now = new Date();
        shopDO.setId(null);
        shopDO.setGmtCreate(now);
        shopDO.setGmtUpdate(now);
        return shopMapper.insert(shopDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteShop(Long userId,Long id) throws ServiceException{
        return shopMapper.delete(new EntityWrapper<ShopDO>()
                .eq("id", id)) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateShop(Long userId,ShopDO shopDO) throws ServiceException{
        if (shopDO == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
               if( shopDO.getUserId() == null ){
                throw new AdminServiceException("unimall的用户id不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
              if( StringUtils.isEmpty(shopDO.getShopName()) ){
                throw new AdminServiceException("商铺名称不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
       if(shopMapper.selectCount(new EntityWrapper<ShopDO>().eq("shop_name",shopDO.getShopName())) > 0){
           throw new AdminServiceException("商铺名称数据库中已存在",ExceptionDefinition.PARAM_IS_ALREADY_EXIST_CODE);
       }
                            
        Date now = new Date();
        shopDO.setGmtUpdate(now);
        return shopMapper.updateById(shopDO) > 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShopDO getShopById(Long userId,Long id) throws ServiceException{

        if(id == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }

        return shopMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ShopDO> getShopList(
                                             Long id,
                                                                                String shopName,
                                                         String phone,
                                                                                                                                                                                                                                                                                                                  Long userId
                    ) throws ServiceException{

        Wrapper<ShopDO> wrapper = new EntityWrapper<ShopDO>();
        wrapper.orderBy("id", false);

                    if( id != null ){
                wrapper.eq("id", id);
            }
                    if( userId != null ){
                wrapper.eq("user_id", userId);
            }
                    if( !StringUtils.isEmpty(shopName) ){
                wrapper.eq("shop_name", shopName);
            }
                    if( !StringUtils.isEmpty(phone) ){
                wrapper.eq("phone", phone);
            }
                                                                                                                                                                                                                                                                                                
        List<ShopDO> ShopDOs = shopMapper.selectList(wrapper);
        return ShopDOs;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page<ShopDO> getShopListWithPage(
           Long userId,
            Long id,
                    String shopName,
                String phone,
     Integer pageNo,Integer limit) throws ServiceException{

        Wrapper<ShopDO> wrapper = new EntityWrapper<ShopDO>();
        wrapper.orderBy("id", false);

    if( id != null ){
            wrapper.eq("id", id);
        }
        if( userId != null ){
            wrapper.eq("user_id", userId);
        }
        if( !StringUtils.isEmpty(shopName) ){
            wrapper.eq("shop_name", shopName);
        }
        if( !StringUtils.isEmpty(phone) ){
            wrapper.eq("phone", phone);
        }
                                                            
        List<ShopDO> ShopDOs = shopMapper.selectPage(new RowBounds((pageNo - 1) * limit, limit), wrapper);
        Integer count = shopMapper.selectCount(wrapper);
        return new Page<ShopDO>(ShopDOs, pageNo, limit, count);
    }
}